﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE.Tool.UserControls;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE.Tool.Forms
{
	public partial class frm_ImportAdd : DevExpress.XtraEditors.XtraForm
	{
		private ucImport parent;
		private DataTable dt = new DataTable();
		private string _makerId = "";

		public frm_ImportAdd(ucImport fImport)
		{
			InitializeComponent();
			parent = fImport;
			this.CancelButton = this.btTCancel;
		}
		
		private void XtraForm1_Load(object sender, EventArgs e)
		{
			rdTool.Checked = true;
			//txtMakerID.Enabled = false;
			dtpTDate.Value = DateTime.Today;
			BindDataSupplier();
			

		}

		private void btTAdd_Click_1(object sender, EventArgs e)
		{
			AddTool();
		}

		private void btTCancel_Click_1(object sender, EventArgs e)
		{
			this.Close();
		}

		private void rdTool_CheckedChanged(object sender, EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;
			BindDataTools(ToolConstants.Configuration.ToolType.IsTool);
			lblMaker.Enabled = false;
			txtMakerID.Text = "";
			Cursor.Current = Cursors.Default;
		}

		private void rdQTool_CheckedChanged(object sender, EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;
			BindDataTools(ToolConstants.Configuration.ToolType.IsQTool);
			lblMaker.Enabled = true;
			Cursor.Current = Cursors.Default;
		}

		private void cmbToolID_SelectionChangeCommitted(object sender, EventArgs e)
		{
			if (rdQTool.Checked)
			{
				GetMakerName(cmbToolID.SelectedValue.ToString());
			}
		}


		#region Private methods

		private bool GetDataTool(Import o)
		{
			#region Check valid

			if(Utilities.IsNullOrEmpty(dtpTDate.Value.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Import.DateCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(cmbToolID.SelectedValue.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Import.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtTPrice.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Import.PriceCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

            if (decimal.Parse(txtTPrice.Text) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.Import.PriceMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTPrice.Focus();
                return false;

            }

            if (Utilities.IsNullOrEmpty(cmbTSupplierID.SelectedValue.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.Import.SupplierCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

			if(Utilities.IsNullOrEmpty(txtTQuantity.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Import.QuantityCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

            if (int.Parse(txtTQuantity.Text) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.Import.QuantityMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTQuantity.Focus();
                return false;

            }


			#endregion

			o.Date = dtpTDate.Value;
			o.ToolID = cmbToolID.SelectedValue.ToString();
			o.MakerID = _makerId;
            o.PriceJPY = decimal.Parse(txtTPrice.Text);
			o.SupplierID = cmbTSupplierID.SelectedValue.ToString();
			o.Quantity = int.Parse(txtTQuantity.Text);
			o.Note = memoTNote.Text;

			return true;
		}

		private void BindDataTools(int type)
		{
			dt.Clear();
			ToolHelper helper = new ToolHelper(new Tools());
			helper.ValueObject.ToolOrQTool = type;
			dt = helper.GetAllByToolType();
			DataRow row = dt.NewRow();

			row[ToolConstants.Entities.Tool.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.Tool.FieldName.ToolID] = null;
			dt.Rows.InsertAt(row, 0);

			cmbToolID.DataSource = dt;
            cmbToolID.DisplayMember = ToolConstants.Entities.Tool.FieldName.FullName;
			cmbToolID.ValueMember = ToolConstants.Entities.Tool.FieldName.ToolID;

			cmbToolID.SelectedItem = cmbToolID.Items[0];
		}

		private void BindDataSupplier()
		{
			SupplierHelper helper = new SupplierHelper(new Supplier());
			DataTable dt = helper.GetAll();
			DataRow row = dt.NewRow();
            row[ToolConstants.Entities.Supplier.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.Supplier.FieldName.SupplierID] = null;
			dt.Rows.InsertAt(row, 0);

			cmbTSupplierID.DataSource = dt;
            cmbTSupplierID.DisplayMember = ToolConstants.Entities.Supplier.FieldName.FullName;
            cmbTSupplierID.ValueMember = ToolConstants.Entities.Supplier.FieldName.SupplierID;

			cmbTSupplierID.SelectedItem = cmbTSupplierID.Items[0];
		}

		private void GetMakerName(string toolId)
		{
			string expression = ToolConstants.Entities.Tool.FieldName.ToolID + " = '"+ toolId +"'";
			DataRow[] foundRows;
			foundRows = dt.Select(expression);
			if (foundRows.Length > 0)
			{
				_makerId = foundRows[0][ToolConstants.Entities.Maker.FieldName.MakerID].ToString();
                txtMakerID.Text = _makerId + " | " + foundRows[0][ToolConstants.Entities.Maker.FieldName.MakerName];
			}
			else
			{
				txtMakerID.Text = "";
			}
		}

		private void AddTool()
		{
			ImportHelper helper = new ImportHelper(new Import());
			if(GetDataTool(helper.ValueObject))
			{

				Utilities.Timer timer = new Utilities.Timer();

				if(helper.Insert() > 0)
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.AddSuccess);

				    dtpTDate.Value = DateTime.Now;
				    rdTool.Checked = true;
				    cmbToolID.SelectedItem = cmbToolID.Items[0];
				    txtTPrice.Text = "";
				    cmbTSupplierID.SelectedItem = cmbTSupplierID.Items[0];
				    txtTQuantity.Text = "";
				    memoTNote.Text = "";
				    dtpTDate.Focus();
				}
				else
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.AddFailed);
				}
				parent._action = 0;
				parent.BindDatatoGrid();

			}
		}


		#endregion

		
	}
}